/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.balysv.materialripple.demo.slice;

import static ohos.agp.components.Component.AXIS_Y;

import com.balysv.materialripple.demo.ResourceTable;
import com.balysv.materialripple.demo.provider.ListXMLProvider;
import com.balysv.materialripple.demo.util.CustomToolbar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;

import java.util.UUID;

public class ListViewAbilitySlice extends AbilitySlice {
    private static final String[] data;

    static {
        data = new String[50];
        for (int i = 0; i < data.length; i++) {
            data[i] = UUID.randomUUID().toString();
        }
    }

    private CustomToolbar customToolbar;
    private ListContainer mListContainerXML;
    private ListXMLProvider listXMLProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_xml_ripple);
        initView();
    }

    private void initView() {
        customToolbar = (CustomToolbar) findComponentById(ResourceTable.Id_toolbar_view);
        customToolbar.getTextListView().setText("RECYCLERVIEW");
        customToolbar.getTextListView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.RecyclerViewAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });
        customToolbar.getTextRecyclerView().setText("BUTTONS");
        customToolbar.getTextRecyclerView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.MainAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });

        mListContainerXML = (ListContainer) findComponentById(ResourceTable.Id_list_xml_ripple);
        mListContainerXML.setScrollbarColor(Color.DKGRAY);
        mListContainerXML.setScrollbarThickness(20);
        mListContainerXML.setScrollbarFadingEnabled(true);
        mListContainerXML.enableScrollBar(AXIS_Y, true);
        mListContainerXML.setScrollbarRoundRect(true);
        mListContainerXML.setScrollbarRadius(10);
        listXMLProvider = new ListXMLProvider(this, data);
        mListContainerXML.setItemProvider(listXMLProvider);
    }
}
